package com.angel.bo.auth.client.config;


import com.angel.bo.auth.client.handler.RequestAttributeHystrixConcurrencyStrategy;
import com.angel.bo.auth.client.interceptor.ServiceFeignInterceptor;
import com.angel.bo.auth.client.interceptor.UserAuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import java.util.ArrayList;
import java.util.Collections;

@Configuration
@Slf4j
public class AuthConfigurer implements WebMvcConfigurer {


    public AuthConfigurer(){
        log.info("AuthConfigurer init");
    }


    @Bean
    UserAuthInterceptor getUserAuthRestInterceptor(){
        return new UserAuthInterceptor();
    }

    private ArrayList<String> getExcludeCommonPathPatterns(){
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/v2/api-docs",
                "/swagger-resources/**",
                "/favicon.ico",
                "/static/**",
                "/api/**",
                "/swagger-ui.html",
                "/webjars/**"

        };
        Collections.addAll(list,urls);
        return list;

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        ArrayList<String> commonPathPatterns = getExcludeCommonPathPatterns();
        registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns("/**").excludePathPatterns(commonPathPatterns.toArray(new String[]{}));
    }

    /**
     * @Author: Haibo.Mei [Haibo.Mei@ge.com]
     * @Description:
     * @Date: 11:34 2017/12/20
     */
    @Bean
    ServiceFeignInterceptor getClientTokenInterceptor(){
        return new ServiceFeignInterceptor();
    }

    @Bean
    public RequestAttributeHystrixConcurrencyStrategy getRequestAttributeHystrixConcurrencyStrategy(){
        return new RequestAttributeHystrixConcurrencyStrategy();
    }

}
